package com.masterclient.app.businessmodule.minemodule.cash.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselib.fragment.FCBaseFragment;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.common.config.BusinessConfig;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.minemodule.cash.adapter.BalanceOrderAdapter;
import com.masterclient.app.businessmodule.minemodule.cash.bean.BalanceCashBean;
import com.masterclient.app.businessmodule.minemodule.cash.bean.BalanceGainBean;
import com.masterclient.app.businessmodule.minemodule.cash.bean.BalanceOrderBean;
import com.masterclient.app.businessmodule.minemodule.cash.contract.CashContract;
import com.masterclient.app.businessmodule.minemodule.cash.contract.CashPresenter;
import com.masterclient.app.businessmodule.minemodule.cash.detail.GainInfoActivity;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * 作者：sean
 * 创建时间：2017/10/12 15:16
 * 功能说明：
 */

public class BalanceOrderFragment extends FCBaseFragment<CashPresenter> implements CashContract.View{

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private BalanceOrderAdapter balanceOrderAdapter;
    private int total;

    @Override
    protected boolean useRefreshLayout() {
        return true;
    }

    @Override
    protected int requireGetLayoutId() {
        return R.layout.fragment_cash;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
            navigationBar.setVisibility(View.GONE);
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        balanceOrderAdapter = new BalanceOrderAdapter();
        recyclerView.setAdapter(balanceOrderAdapter);
        balanceOrderAdapter.bindToRecyclerView(recyclerView);

        mPresenter.getBalanceOrder("1", BusinessConfig.PAGE_SIZE,false);

        baseRefreshLayout().setOnRefreshListener(()->{
            if(mPresenter.orderList != null){
                mPresenter.orderList.clear();
                balanceOrderAdapter.notifyDataSetChanged();
            }
            mPresenter.getBalanceOrder("1",BusinessConfig.PAGE_SIZE,true);
        });

        balanceOrderAdapter.setOnLoadMoreListener(()->{
            if(mPresenter.orderList.size()<total){
                mPresenter.getBalanceOrder(String.valueOf(mPresenter.orderList.size()/Integer.valueOf(BusinessConfig.PAGE_SIZE)+1),BusinessConfig.PAGE_SIZE,false);
            }
        },recyclerView);
        balanceOrderAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivitryWithBundle(GainInfoActivity.class,new String[][]{{"id",mPresenter.orderList.get(position).getSh_id()},{"type","2"}});
            }
        });
    }

    @Override
    public void showBalanceGain(BalanceGainBean.DataBean bean) {

    }

    @Override
    public void showBalanceOrder(BalanceOrderBean.DataBean bean) {

        if(baseRefreshLayout()!=null&&baseRefreshLayout().isRefreshing()){
            baseRefreshLayout().setRefreshing(false);
        }

        total = Integer.valueOf(bean.getSh_total_rows());
        if(mPresenter.orderList == null){
            mPresenter.orderList = new ArrayList<>();
        }
        mPresenter.orderList.addAll(bean.getSh_items());
        balanceOrderAdapter.setNewData(mPresenter.orderList);
        if(mPresenter.orderList.size()>=total){
            balanceOrderAdapter.setEnableLoadMore(false);
        }
    }

    @Override
    public void showBalanceCash(BalanceCashBean.DataBean bean) {

    }

    @Override
    public void loadFailed() {
        if(baseRefreshLayout()!=null && baseRefreshLayout().isRefreshing()){
            baseRefreshLayout().setRefreshing(false);
        }
        if (mPresenter.orderList!=null && (mPresenter.orderList.size()/Integer.valueOf(BusinessConfig.PAGE_SIZE)+1) > 1) {
            balanceOrderAdapter.loadMoreFail();
        }
    }
}
